package zhihui.mobile.manager.register;


import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zhihui.mobile.bean.register.RegisterInfo;
import zhihui.mobile.bean.address.CommunityInfo;
import zhihui.mobile.bean.retrieve.RetrieveInfo;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.AllManagerObvs;
import zhihui.mobile.manager.IManager;
import zhihui.mobile.net.HttpUtils;

/**
 * Created by 张公铭 on 2017/7/6 0006.
 */

public class RegisterManager implements IManager
{
    private Logger logger = Logger.getLogger(RegisterManager.class);
    private static RegisterManager manager;
    private RegisterInfo curRegister = new RegisterInfo();
    private CommunityInfo curCommunityInfo;

    private RegisterManager()
    {
        AllManagerObvs.getInstance().addManager(this);
    }

    private synchronized static void createdIns()
    {
        if (manager == null)
        {
            manager = new RegisterManager();
        }
    }

    public static RegisterManager getInstance()
    {
        if (manager == null)
        {
            createdIns();
        }
        return manager;
    }

    public RegisterInfo getCurRegister()
    {
        return curRegister;
    }

    public CommunityInfo getCurCommunityInfo()
    {
        return curCommunityInfo;
    }

    public void setCurCommunityInfo(CommunityInfo curCommunityInfo)
    {
        this.curCommunityInfo = curCommunityInfo;
    }

    public void register(IHttpCallBackListener listener)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "user");
        params.put("method", "regist");

        List<Object> values = new ArrayList<>();
        values.add(curRegister.getPhone());
        values.add(curRegister.getCode());
        values.add(curRegister.getPassword());
        values.add(Config.deviceId);
        values.add(Config.deviceId);
        values.add(curRegister.getCommunityId());

        HttpUtils.postAes(Config.URL, params, values, listener);
    }

    public void retrievePassword(RetrieveInfo info, IHttpCallBackListener listener)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "user");
        params.put("method", "findpwd");

        List<Object> values = new ArrayList<>();
        values.add(info.getPhone());
        values.add(info.getPassword());
        values.add(info.getCede());

        HttpUtils.postAes(Config.URL, params, values, listener);
    }

    @Override
    public void clear()
    {
        AllManagerObvs.getInstance().removeManager(this);
    }
}
